package com.elric.mall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.elric.mall.dto.FundingInvestmentDto;
import com.elric.mall.dto.ProcessInfoDto;
import com.elric.mall.dto.ProjectBaseInfoDto;
import com.elric.mall.mapper.*;
import com.elric.mall.model.*;
import com.elric.mall.service.ZgProjectEntityNewService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 实现功能：规划项目信息实现层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:21
 */
@Service
public class ZgProjectEntityNewServiceImpl implements ZgProjectEntityNewService {
    @Autowired
    private ZgProjectEntityNewMapper zgProjectEntityNewMapper;

    @Autowired
    private ZgLeadDoorMapper zgLeadDoorMapper;

    @Autowired
    private ZgFundingInvestmentMapper zgFundingInvestmentMapper;

    @Autowired
    private ZgConstructionPeriodMapper zgConstructionPeriodMapper;

    @Autowired
    private ZgProjectImplementationMapper zgProjectImplementationMapper;

    @Override
    public int create(ZgProjectEntityNew zgProjectEntityNew) {
        zgProjectEntityNew.setId(IdUtil.fastUUID());
        return zgProjectEntityNewMapper.insert(zgProjectEntityNew);
    }

    @Override
    public int update(ZgProjectEntityNew zgProjectEntityNew) {
        return zgProjectEntityNewMapper.updateByPrimaryKeySelective(zgProjectEntityNew);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveBaseInfo(ProjectBaseInfoDto projectBaseInfoDto, UmsAdmin umsAdmin) {
        ZgProjectEntityNew zgProjectEntityNew = new ZgProjectEntityNew();
        BeanUtils.copyProperties(projectBaseInfoDto, zgProjectEntityNew);
        if (StrUtil.isBlank(zgProjectEntityNew.getId())) {
            zgProjectEntityNew.setId(IdUtil.fastUUID());
            zgProjectEntityNew.setCreateId(umsAdmin.getId());
            zgProjectEntityNew.setCreateTime(new DateTime());
            zgProjectEntityNew.setUpdateTime(new DateTime());
            zgProjectEntityNewMapper.insert(zgProjectEntityNew);
        } else {
            zgProjectEntityNew.setUpdateTime(new DateTime());
            zgProjectEntityNewMapper.updateByPrimaryKey(zgProjectEntityNew);
        }
        ZgLeadDoorExample example = new ZgLeadDoorExample();
        example.createCriteria().andProjectIdEqualTo(zgProjectEntityNew.getId()).andCreateIdEqualTo(zgProjectEntityNew.getCreateId());
        // 先清空旧的关联数据再插入
        zgLeadDoorMapper.deleteByExample(example);
        projectBaseInfoDto.getGlbmList().stream().forEach(p -> {
            p.setId(IdUtil.fastUUID());
            p.setProjectId(zgProjectEntityNew.getId());
            p.setCreateId(zgProjectEntityNew.getCreateId());
        });
        return zgLeadDoorMapper.insertBatch(projectBaseInfoDto.getGlbmList());
    }

    @Override
    public ZgProjectEntityNew findById(String id, String createId) {
        ProjectBaseInfoDto projectBaseInfoDto = new ProjectBaseInfoDto();
        ZgProjectEntityNew zgProjectEntityNew = zgProjectEntityNewMapper.selectByPrimaryKey(id, createId);
        BeanUtils.copyProperties(zgProjectEntityNew, projectBaseInfoDto);
        // 根据项目id和创建人id查询管理部门相关信息
        ZgLeadDoorExample example = new ZgLeadDoorExample();
        example.createCriteria().andProjectIdEqualTo(id).andCreateIdEqualTo(createId);
        List<ZgLeadDoor> leadDoors = zgLeadDoorMapper.selectByExample(example);
        projectBaseInfoDto.setGlbmList(leadDoors);
        return projectBaseInfoDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(String id, String createId) {
        zgProjectEntityNewMapper.deleteByPrimaryKey(id, createId);
        // 根据项目id和创建人id查询管理部门相关信息
        ZgLeadDoorExample example = new ZgLeadDoorExample();
        example.createCriteria().andProjectIdEqualTo(id).andCreateIdEqualTo(createId);
        zgLeadDoorMapper.deleteByExample(example);
        return 1;
    }

    @Override
    public List<ZgProjectEntityNew> list(Integer pageSize, Integer pageNum, ZgProjectEntityNew zgProjectEntityNew) {
        PageHelper.startPage(pageNum, pageSize);
        ZgProjectEntityNewExample example = new ZgProjectEntityNewExample();
        if(!StringUtils.isEmpty(zgProjectEntityNew.getProjectName())) {
            example.createCriteria().andProjectNameLike("%"+zgProjectEntityNew.getProjectName()+ "%");
        }
        example.setOrderByClause("create_time desc");
        return zgProjectEntityNewMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveFundingInfo(FundingInvestmentDto dto) {
        if (StrUtil.isNotBlank(dto.getCreateId())&&StrUtil.isNotBlank(dto.getProjectId())) {
            // 先清空往期经费投入数据
            ZgFundingInvestmentExample example = new ZgFundingInvestmentExample();
            example.createCriteria().andProjectIdEqualTo(dto.getProjectId())
                    .andCreateIdEqualTo(dto.getCreateId());
            zgFundingInvestmentMapper.deleteByExample(example);

            if (CollectionUtil.isNotEmpty(dto.getList())) {
                // 重新设置id、项目id等信息
                dto.getList().stream().forEach(f -> {
                    f.setId(IdUtil.fastUUID());
                    f.setProjectId(dto.getProjectId());
                    f.setCreateId(dto.getCreateId());
                });
                return zgFundingInvestmentMapper.insertBatch(dto.getList());
            }
        }
        return 1;
    }

    @Override
    public List<ZgFundingInvestment> findFundingInfo(String projectId, String createId) {
        ZgFundingInvestmentExample example = new ZgFundingInvestmentExample();
        example.createCriteria().andCreateIdEqualTo(createId)
                .andProjectIdEqualTo(projectId);
        return zgFundingInvestmentMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveProcessInfo(ProcessInfoDto dto) {
        ZgConstructionPeriod period = new ZgConstructionPeriod();
        BeanUtils.copyProperties(dto, period);
        if (StrUtil.isNotBlank(period.getId())) {
            zgConstructionPeriodMapper.updateByPrimaryKey(period);
        } else {
            period.setId(IdUtil.fastUUID());
            zgConstructionPeriodMapper.insert(period);
        }
        // 先清空老拓展表数据
        ZgProjectImplementationExample example = new ZgProjectImplementationExample();
        example.createCriteria().andProjectIdEqualTo(dto.getProjectId())
                .andCreateIdEqualTo(dto.getCreateId());
        zgProjectImplementationMapper.deleteByExample(example);
        dto.getImplementationList().stream().forEach(i -> {
            i.setId(IdUtil.fastUUID());
            i.setConstructionPeriodId(period.getId());
            i.setProjectId(period.getProjectId());
            i.setCreateId(period.getCreateId());
        });
        zgProjectImplementationMapper.insertBatch(dto.getImplementationList());
        return 1;
    }

    @Override
    public ProcessInfoDto findProcessInfo(String projectId, String createId) {
        ZgConstructionPeriodExample periodExample = new ZgConstructionPeriodExample();
        periodExample.createCriteria().andProjectIdEqualTo(projectId)
                .andCreateIdEqualTo(createId);
        List<ZgConstructionPeriod> list = zgConstructionPeriodMapper.selectByExample(periodExample);
        if (CollectionUtil.isNotEmpty(list)) {
            ProcessInfoDto dto = new ProcessInfoDto();
            ZgConstructionPeriod period = list.get(0);
            BeanUtils.copyProperties(period, dto);
            ZgProjectImplementationExample implementationExample = new ZgProjectImplementationExample();
            implementationExample.createCriteria().andProjectIdEqualTo(projectId)
                    .andCreateIdEqualTo(createId);
            dto.setImplementationList(zgProjectImplementationMapper.selectByExample(implementationExample));
            return dto;
        }
        return null;
    }
}
